/*
* Copyright (c) 2016, Spreadtrum Communications.
*
* The above copyright notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sprd_hash.h>
#include <sprd_rsa.h>

void sha256_csum_wd(const unsigned char *input,unsigned int ilen,unsigned char *output,unsigned int chunk_sz) {
	sprd_crypto_context_t context;
	sprd_crypto_err_t err = SPRD_CRYPTO_SUCCESS;

	err = sprd_hash_init(SPRD_CRYPTO_HASH_SHA256, &context);
	if (err != SPRD_CRYPTO_SUCCESS) {
		errorf("sha256_csum_wd: sprd_hash_init return [0x%x]\n", err);
		goto failed;
	}

	err = sprd_hash_update(&context, input, ilen);
	if (err != SPRD_CRYPTO_SUCCESS) {
		errorf("sha256_csum_wd: sprd_hash_process return [0x%x]\n", err);
		goto failed;
	}

	err = sprd_hash_final(&context, output);
	if (err != SPRD_CRYPTO_SUCCESS) {
		errorf("sha256_csum_wd: sprd_hash_final return [0x%x]\n", err);
		goto failed;
	}

failed:
	return;
}

int RSA_PubDec(unsigned char *pub_E, unsigned char *mod_N, int bitLen_N, unsigned char *from, unsigned char *to) {
	sprd_crypto_err_t err = SPRD_CRYPTO_SUCCESS;
	sprd_rsa_pubkey_t pubkey;
	sprd_rsa_padding_t padding;
	int32_t result;

	pubkey.n = mod_N;
	pubkey.n_len = bitLen_N >> 3;
	pubkey.e = pub_E;
	pubkey.e_len = 4;
	padding.type = SPRD_RSASSA_PKCS1_V1_5;
	err = sprd_rsa_verify((const sprd_rsa_pubkey_t*)(&pubkey),
			to, 0,
			(const uint8_t*)(from), pubkey.n_len,
			padding, &result);
	if (err != SPRD_CRYPTO_SUCCESS) {
		errorf("sprd_rsa_verify fail(%08x)\n", err);
		return -1;
	}

	if (result <= SPRD_VERIFY_SUCCESS) {
		errorf("sprd_rsa_verify result(%08x)\n", result);
		return -1;
	}

	return result;
}

int sprd_crypto_special_test(void) {
	static const struct {
		const char *msg;
		unsigned char hash[32];
	} test_hash = {
		"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678901"
		"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678901"
		"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678901"
		"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz012345678901",//256
		{ 0x3e, 0x36, 0x83, 0x70, 0x58, 0xcc, 0xe4, 0xdb,
		  0xb5, 0x6c, 0x5d, 0x7e, 0x61, 0xff, 0x37, 0xf0,
		  0xdc, 0x0a, 0x0e, 0x84, 0x31, 0xa4, 0xf9, 0x35,
		  0xc0, 0x0d, 0x0e, 0x3d, 0x61, 0x19, 0x83, 0xa0 }
	};

	static struct {
		uint32_t data_len;
		uint32_t key_len;
		uint8_t key_n[512];
		uint8_t key_e[4];
		uint8_t key_d[512];
		uint8_t din[512];
		uint8_t dout[512];
	} test_rsa_pkcs1 =
	{ //2048
		32,
		256,
		//n
		{ 0xBB, 0xE6, 0x11, 0x28, 0x13, 0x40, 0xD8, 0x6D, 0xE9, 0x54, 0x78, 0x82, 0x01, 0xA0, 0xDE, 0xCE,
		  0x2D, 0x57, 0x69, 0x8E, 0x8A, 0x93, 0x26, 0x4F, 0x72, 0x3F, 0xEB, 0xEF, 0xE1, 0xE9, 0xF9, 0x2E,
		  0x6F, 0x43, 0x47, 0x0A, 0x4B, 0xA0, 0x83, 0xFC, 0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 0xbf, 0x42, 0x02, 0x30, 0xf7, 0x65, 0x34, 0x42, 0x72, 0x55,
		  0x86, 0x76, 0xd5, 0x69, 0xe4, 0xd3, 0xf7, 0xb3, 0x48, 0x9b, 0x6b, 0x13, 0x89, 0x16, 0x84, 0xcb,
		  0xbe, 0x35, 0x97, 0x9a, 0xeb, 0x38, 0x39, 0x00, 0x17, 0x22, 0xf4, 0x6f, 0x72, 0x01, 0x41, 0x3e,
		  0x3f, 0x8e, 0x2b, 0x10, 0x9b, 0xfd, 0x84, 0x1e, 0xea, 0xa6, 0xb5, 0x01, 0x47, 0x25, 0xe6, 0x55,
		  0xde, 0x0d, 0xc6, 0xe9, 0xb1, 0x47, 0x26, 0xa6, 0xcc, 0xee, 0xdc, 0x02, 0x7a, 0x0e, 0x6b, 0xd9,
		  0x66, 0x08, 0xd3, 0x5c, 0x25, 0x0e, 0x49, 0x8f, 0x6d, 0x96, 0x34, 0x0d, 0xd0, 0xd8, 0x8d, 0x83,
		  0x7d, 0xae, 0x3a, 0x85, 0x14, 0xcd, 0x60, 0xcc, 0x71, 0xfa, 0xf2, 0x36, 0x55, 0xd2, 0x57, 0xb4,
		  0x5b, 0x15, 0x75, 0x06, 0xd1, 0x59, 0x36, 0xb0, 0xd3, 0x64, 0x8c, 0x94, 0x55, 0xe7, 0x17, 0x2b,
		  0x91, 0xef, 0x38, 0x9f, 0x55, 0x72, 0xa6, 0x4e, 0x8e, 0x4e, 0xbb, 0x65, 0x1a, 0xbb, 0x13, 0xc6,
		  0xb6, 0x2d, 0xd7, 0xd5, 0xd7, 0xc1, 0xac, 0x62, 0xa3, 0x0f, 0x34, 0xb4, 0x17, 0x8d, 0x16, 0x70,
		  0x3b, 0x6c, 0xec, 0x1b, 0x3c, 0xea, 0xb0, 0xaf, 0xdc, 0x30, 0xf0, 0x37, 0xe3, 0x25, 0xa7, 0xd3,
		  0x39, 0x68, 0xdb, 0x96, 0x2c, 0x05, 0xd2, 0x9a, 0xb5, 0x6e, 0x30, 0x9c, 0xdb, 0x98, 0xc0, 0x14,
		  0xf1, 0x4f, 0x58, 0xb0, 0x2d, 0x77, 0x42, 0x68, 0x70, 0x58, 0x96, 0x95, 0x4a, 0xad, 0xa6, 0xa9,
		  0x43, 0x3b, 0x52, 0xd6, 0x15, 0xd1, 0xf9, 0x36, 0x17, 0xa2, 0xe1, 0x4d, 0x05, 0x32, 0xc2, 0xbe,
		  0x40, 0x26, 0x97, 0xf4, 0xbe, 0x9b, 0x6a, 0x39, 0x7d, 0x85, 0xc9, 0x94, 0x6f, 0x24, 0x63, 0x58, },
	};

	int err = 0;
	uint8_t hash_temp[32];
	uint8_t rsa_temp[32];
#define SPRD_TEST_ADDR 0x96000000

	sha256_csum_wd((const unsigned char*)test_hash.msg, strlen(test_hash.msg), (unsigned char*)hash_temp, 0);
	if (memcmp(test_hash.hash, hash_temp, 32)) {
		errorf("sha256_csum_wd failed!\n");
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x10)) = 0x11111111;
		return -1;
	}
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x10)) = 0xAAAAAAAA;

	err = RSA_PubDec(test_rsa_pkcs1.key_e,
			 test_rsa_pkcs1.key_n,
			 test_rsa_pkcs1.key_len * 8,
			 test_rsa_pkcs1.dout, rsa_temp);
	if (err != test_rsa_pkcs1.data_len || memcmp(test_rsa_pkcs1.din, rsa_temp, err)){
		errorf("RSA_PubDec failed!\n");
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x20)) = 0x22222222;
		return -1;
	}
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x20)) = 0xBBBBBBBB;

	test_rsa_pkcs1.dout[0] += 1;
	err = RSA_PubDec(test_rsa_pkcs1.key_e,
			 test_rsa_pkcs1.key_n,
			 test_rsa_pkcs1.key_len * 8,
			 test_rsa_pkcs1.dout, rsa_temp);
	if (err != -1){
		errorf("RSA_PubDec should be failed!\n");
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x30)) = 0x33333333;
		return -1;
	}
	*((volatile unsigned int*)(SPRD_TEST_ADDR + 0x30)) = 0xCCCCCCCC;

	errorf("sprd_crypto_special_test pass!\n");
	return 0;
}
